﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace linkDATABASE.留言
{
    public partial class lleavemessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string deti = TextBox2.Text;
            string time = DateTime.Now.ToString();
            if (!name.Equals("") && !deti.Equals(""))
            {
                string str = "Data Source=101.200.157.164;Initial Catalog=GuestBook;Persist Security Info=True;User ID=sa;Password=Jinghaoyang1";//设置连接字符串
                                                                                                                                                 //创建一个SqlConnection对象
                SqlConnection myConnection = new SqlConnection(str);
                myConnection.Open();//打开数据库连接
                string sqlstr = "INSERT INTO GuestBook.dbo.gusetbookinfo(Name,Detail,Time) " +
                    "VALUES('" + name + "', '" + deti + "', '" + time + "')";
                //创建添加留言信息的sql语句
                SqlCommand com = new SqlCommand(sqlstr, myConnection);
                com.ExecuteNonQuery();//执行数据添加操作
                myConnection.Close();

                Response.Write("<script>alert('恭喜您，添加留言已成功! ')</script>");

                TextBox1.Text = "";
                TextBox2.Text = "";
            }
            else
            {
                Response.Write("<script>alert('名字和内容不可为空 ')</script>");
            }

            

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("../留言查询/querymessages.aspx");
        }
    }
}